<template>
	<div class="scanCode" v-show="!item.common.isHidden" @tap="handleTap" :style="commonStyle+innerStyle"></div>
</template>

<script>
	import ajax from '../libs/ajax.js';
	import calcBaseStyle from '../libs/calcBaseStyle'
	export default {
		name: 'scanCode',
		props:{
			pageConfig: {
				type: Object,
				default(){
					return {}
				}
			},
			pcid: null
		},
		data(){
			return {
				item: {common: {},style: {},children:[]},
			}
		},
		created() {
			this.$parent.widgetList.some(widget=>{
				if(widget.cid === this.pcid){
					this.item = widget
					return true
				}
			})
		},
		computed:{
			commonStyle(){
				return calcBaseStyle(this.item,this.pageConfig.contentH)
			},
			innerStyle(){
				return 'background-image:url('+this.item.common.bgImg+');'
			},
		},
		methods:{
			handleTap(){
				var scanCode = this.item
				uni.scanCode({
					success: res => {
						console.log('扫码结果', res);
						const url = res.result;
						if (scanCode.common.fnType === 'login' && res.result.indexOf(scanCode.common.loginKeyWord) != -1) {
							uni.showModal({
								title: '温馨提示',
								content: '是否扫码登录？',
								success: res => {
									if (res.confirm) {
										ajax({
											url,
											needAuth: true,
											method: 'get'
										}).then(res => {
											if (res.code == 200) {
												uni.showToast({
													title: '登录成功',
													icon: 'none'
												});
											}
										});
									}
								}
							});
						}
					}
				});
			},
		}
	}
</script>

<style lang="less" scoped>
	.scanCode {
		position: absolute;
		background-repeat: no-repeat;
		background-size: 100% 100%;
		background-position: center center;
		overflow: hidden;
	}
</style>
